<template>
  <div>
    <el-button type="success" @click="exporFile">导出</el-button>
  </div>
</template>
<script>
import { wlcSysAllCheckOut, wlcSysInquiryCheckOut } from "../../api/api";
import moment from "moment";
export default {
  data() {
    return {};
  },
  props: ["sendData", "url", "state"],
  methods: {
    exporFile() {
      if (this.sendData.id && this.sendData.type) {
        if (this.state == 1) {
          this.purchExporForm();
        }
        if (this.state == 2) {
          this.inquiryExporForm();
        }
      } else {
        this.$message({
          type: "warning",
          message: "暂无数据"
        });
      }
    },
    purchExporForm() {
      let param = {
        id: this.sendData.id,
        // allId:this.sendData.id,
        type: this.sendData.type
      };
      wlcSysAllCheckOut(param).then(res => {
        let blob = res.data;
        let reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onload = e => {
          let a = document.createElement("a");
          let name =
            "请购单" +
            this.sendData.orderNum +
            "_" +
            moment().format("YYYY-MM-DD HH:mm:ss") +
            ".xlsx";
          a.download = name;
          a.href = e.target.result;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        };
      });
    },
    inquiryExporForm() {
      let param = {
        // id: this.sendData.id,
        allId: this.sendData.id,
        type: this.sendData.type
      };
      wlcSysInquiryCheckOut(param).then(res => {
        let blob = res.data;
        let reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onload = e => {
          let a = document.createElement("a");
          let name =
            "询价单" +
            this.sendData.orderNum +
            "_" +
            moment().format("YYYY-MM-DD HH:mm:ss") +
            ".xlsx";
          a.download = name;
          a.href = e.target.result;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        };
      });
    }
  }
};
</script>
<style lang="less" scoped>
</style>